home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / BaseClasses / wxlist.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  19.2 KB  |  552 lines

  1. //------------------------------------------------------------------------------
  2. // File: WXList.h
  3. //
  4. // Desc: DirectShow base classes - defines a non-MFC generic template list
  5. //       class.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10.  
  11. /* A generic list of pointers to objects.
  12.    No storage management or copying is done on the objects pointed to.
  13.    Objectives: avoid using MFC libraries in ndm kernel mode and
  14.    provide a really useful list type.
  15.  
  16.    The class is thread safe in that separate threads may add and
  17.    delete items in the list concurrently although the application
  18.    must ensure that constructor and destructor access is suitably
  19.    synchronised. An application can cause deadlock with operations
  20.    which use two lists by simultaneously calling
  21.    list1->Operation(list2) and list2->Operation(list1).  So don't!
  22.  
  23.    The names must not conflict with MFC classes as an application
  24.    may use both.
  25.    */
  26.  
  27. #ifndef __WXLIST__
  28. #define __WXLIST__
  29.  
  30.    /* A POSITION represents (in some fashion that's opaque) a cursor
  31.       on the list that can be set to identify any element.  NULL is
  32.       a valid value and several operations regard NULL as the position
  33.       "one step off the end of the list".  (In an n element list there
  34.       are n+1 places to insert and NULL is that "n+1-th" value).
  35.       The POSITION of an element in the list is only invalidated if
  36.       that element is deleted.  Move operations may mean that what
  37.       was a valid POSITION in one list is now a valid POSITION in
  38.       a different list.
  39.  
  40.       Some operations which at first sight are illegal are allowed as
  41.       harmless no-ops.  For instance RemoveHead is legal on an empty
  42.       list and it returns NULL.  This allows an atomic way to test if
  43.       there is an element there, and if so, get it.  The two operations
  44.       AddTail and RemoveHead thus implement a MONITOR (See Hoare's paper).
  45.  
  46.       Single element operations return POSITIONs, non-NULL means it worked.
  47.       whole list operations return a BOOL.  TRUE means it all worked.
  48.  
  49.       This definition is the same as the POSITION type for MFCs, so we must
  50.       avoid defining it twice.
  51.    */
  52. #ifndef __AFX_H__
  53. struct __POSITION { int unused; };
  54. typedef __POSITION* POSITION;
  55. #endif
  56.  
  57. const int DEFAULTCACHE = 10;    /* Default node object cache size */
  58.  
  59. /* A class representing one node in a list.
  60.    Each node knows a pointer to it's adjacent nodes and also a pointer
  61.    to the object that it looks after.
  62.    All of these pointers can be retrieved or set through member functions.
  63. */
  64. class CBaseList 
  65. #ifdef DEBUG
  66.     : public CBaseObject
  67. #endif
  68. {
  69.     /* Making these classes inherit from CBaseObject does nothing
  70.        functionally but it allows us to check there are no memory
  71.        leaks in debug builds. 
  72.     */
  73.  
  74. public:
  75.  
  76. #ifdef DEBUG
  77.     class CNode : public CBaseObject {
  78. #else
  79.     class CNode {
  80. #endif
  81.  
  82.         CNode *m_pPrev;         /* Previous node in the list */
  83.         CNode *m_pNext;         /* Next node in the list */
  84.         void *m_pObject;      /* Pointer to the object */
  85.  
  86.     public:
  87.  
  88.         /* Constructor - initialise the object's pointers */
  89.         CNode()
  90. #ifdef DEBUG
  91.             : CBaseObject(NAME("List node"))
  92. #endif
  93.         {
  94.         };
  95.  
  96.  
  97.         /* Return the previous node before this one */
  98.         CNode *Prev() const { return m_pPrev; };
  99.  
  100.  
  101.         /* Return the next node after this one */
  102.         CNode *Next() const { return m_pNext; };
  103.  
  104.  
  105.         /* Set the previous node before this one */
  106.         void SetPrev(CNode *p) { m_pPrev = p; };
  107.  
  108.  
  109.         /* Set the next node after this one */
  110.         void SetNext(CNode *p) { m_pNext = p; };
  111.  
  112.  
  113.         /* Get the pointer to the object for this node */
  114.         void *GetData() const { return m_pObject; };
  115.  
  116.  
  117.         /* Set the pointer to the object for this node */
  118.         void SetData(void *p) { m_pObject = p; };
  119.     };
  120.  
  121.     class CNodeCache
  122.     {
  123.     public:
  124.         CNodeCache(INT iCacheSize) : m_iCacheSize(iCacheSize),
  125.                                      m_pHead(NULL),
  126.                                      m_iUsed(0)
  127.                                      {};
  128.         ~CNodeCache() {
  129.             CNode *pNode = m_pHead;
  130.             while (pNode) {
  131.                 CNode *pCurrent = pNode;
  132.                 pNode = pNode->Next();
  133.                 delete pCurrent;
  134.             }
  135.         };
  136.         void AddToCache(CNode *pNode)
  137.         {
  138.             if (m_iUsed < m_iCacheSize) {
  139.                 pNode->SetNext(m_pHead);
  140.                 m_pHead = pNode;
  141.                 m_iUsed++;
  142.             } else {
  143.                 delete pNode;
  144.             }
  145.         };
  146.         CNode *RemoveFromCache()
  147.         {
  148.             CNode *pNode = m_pHead;
  149.             if (pNode != NULL) {
  150.                 m_pHead = pNode->Next();
  151.                 m_iUsed--;
  152.                 ASSERT(m_iUsed >= 0);
  153.             } else {
  154.                 ASSERT(m_iUsed == 0);
  155.             }
  156.             return pNode;
  157.         };
  158.     private:
  159.         INT m_iCacheSize;
  160.         INT m_iUsed;
  161.         CNode *m_pHead;
  162.     };
  163.  
  164. protected:
  165.  
  166.     CNode* m_pFirst;    /* Pointer to first node in the list */
  167.     CNode* m_pLast;     /* Pointer to the last node in the list */
  168.     LONG m_Count;       /* Number of nodes currently in the list */
  169.  
  170. private:
  171.  
  172.     CNodeCache m_Cache; /* Cache of unused node pointers */
  173.  
  174. private:
  175.  
  176.     /* These override the default copy constructor and assignment
  177.        operator for all list classes. They are in the private class
  178.        declaration section so that anybody trying to pass a list
  179.        object by value will generate a compile time error of
  180.        "cannot access the private member function". If these were
  181.        not here then the compiler will create default constructors
  182.        and assignment operators which when executed first take a
  183.        copy of all member variables and then during destruction
  184.        delete them all. This must not be done for any heap
  185.        allocated data.
  186.     */
  187.     CBaseList(const CBaseList &refList);
  188.     CBaseList &operator=(const CBaseList &refList);
  189.  
  190. public:
  191.  
  192.     CBaseList(TCHAR *pName,
  193.               INT iItems);
  194.  
  195.     CBaseList(TCHAR *pName);
  196. #ifdef UNICODE
  197.     CBaseList(CHAR *pName,
  198.               INT iItems);
  199.  
  200.     CBaseList(CHAR *pName);
  201. #endif
  202.     ~CBaseList();
  203.  
  204.     /* Remove all the nodes from *this i.e. make the list empty */
  205.     void RemoveAll();
  206.  
  207.  
  208.     /* Return a cursor which identifies the first element of *this */
  209.     POSITION GetHeadPositionI() const;
  210.  
  211.  
  212.     /* Return a cursor which identifies the last element of *this */
  213.     POSITION GetTailPositionI() const;
  214.  
  215.  
  216.     /* Return the number of objects in *this */
  217.     int GetCountI() const;
  218.  
  219. protected:
  220.     /* Return the pointer to the object at rp,
  221.        Update rp to the next node in *this
  222.        but make it NULL if it was at the end of *this.
  223.        This is a wart retained for backwards compatibility.
  224.        GetPrev is not implemented.
  225.        Use Next, Prev and Get separately.
  226.     */
  227.     void *GetNextI(POSITION& rp) const;
  228.  
  229.  
  230.     /* Return a pointer to the object at p
  231.        Asking for the object at NULL will return NULL harmlessly.
  232.     */
  233.     void *GetI(POSITION p) const;
  234.  
  235. public:
  236.     /* return the next / prev position in *this
  237.        return NULL when going past the end/start.
  238.        Next(NULL) is same as GetHeadPosition()
  239.        Prev(NULL) is same as GetTailPosition()
  240.        An n element list therefore behaves like a n+1 element
  241.        cycle with NULL at the start/end.
  242.  
  243.        !!WARNING!! - This handling of NULL is DIFFERENT from GetNext.
  244.  
  245.        Some reasons are:
  246.        1. For a list of n items there are n+1 positions to insert
  247.           These are conveniently encoded as the n POSITIONs and NULL.
  248.        2. If you are keeping a list sorted (fairly common) and you
  249.           search forward for an element to insert before and don't
  250.           find it you finish up with NULL as the element before which
  251.           to insert.  You then want that NULL to be a valid POSITION
  252.           so that you can insert before it and you want that insertion
  253.           point to mean the (n+1)-th one that doesn't have a POSITION.
  254.           (symmetrically if you are working backwards through the list).
  255.        3. It simplifies the algebra which the methods generate.
  256.           e.g. AddBefore(p,x) is identical to AddAfter(Prev(p),x)
  257.           in ALL cases.  All the other arguments probably are reflections
  258.           of the algebraic point.
  259.     */
  260.     POSITION Next(POSITION pos) const
  261.     {
  262.         if (pos == NULL) {
  263.             return (POSITION) m_pFirst;
  264.         }
  265.         CNode *pn = (CNode *) pos;
  266.         return (POSITION) pn->Next();
  267.     } //Next
  268.  
  269.     // See Next
  270.     POSITION Prev(POSITION pos) const
  271.     {
  272.         if (pos == NULL) {
  273.             return (POSITION) m_pLast;
  274.         }
  275.         CNode *pn = (CNode *) pos;
  276.         return (POSITION) pn->Prev();
  277.     } //Prev
  278.  
  279.  
  280.     /* Return the first position in *this which holds the given
  281.        pointer.  Return NULL if the pointer was not not found.
  282.     */
  283. protected:
  284.     POSITION FindI( void * pObj) const;
  285.  
  286.     // ??? Should there be (or even should there be only)
  287.     // ??? POSITION FindNextAfter(void * pObj, POSITION p)
  288.     // ??? And of course FindPrevBefore too.
  289.     // ??? List.Find(&Obj) then becomes List.FindNextAfter(&Obj, NULL)
  290.  
  291.  
  292.     /* Remove the first node in *this (deletes the pointer to its
  293.        object from the list, does not free the object itself).
  294.        Return the pointer to its object.
  295.        If *this was already empty it will harmlessly return NULL.
  296.     */
  297.     void *RemoveHeadI();
  298.  
  299.  
  300.     /* Remove the last node in *this (deletes the pointer to its
  301.        object from the list, does not free the object itself).
  302.        Return the pointer to its object.
  303.        If *this was already empty it will harmlessly return NULL.
  304.     */
  305.     void *RemoveTailI();
  306.  
  307.  
  308.     /* Remove the node identified by p from the list (deletes the pointer
  309.        to its object from the list, does not free the object itself).
  310.        Asking to Remove the object at NULL will harmlessly return NULL.
  311.        Return the pointer to the object removed.
  312.     */
  313.     void *RemoveI(POSITION p);
  314.  
  315.     /* Add single object *pObj to become a new last element of the list.
  316.        Return the new tail position, NULL if it fails.
  317.        If you are adding a COM objects, you might want AddRef it first.
  318.        Other existing POSITIONs in *this are still valid
  319.     */
  320.     POSITION AddTailI(void * pObj);
  321. public:
  322.  
  323.  
  324.     /* Add all the elements in *pList to the tail of *this.
  325.        This duplicates all the nodes in *pList (i.e. duplicates
  326.        all its pointers to objects).  It does not duplicate the objects.
  327.        If you are adding a list of pointers to a COM object into the list
  328.        it's a good idea to AddRef them all  it when you AddTail it.
  329.        Return TRUE if it all worked, FALSE if it didn't.
  330.        If it fails some elements may have been added.
  331.        Existing POSITIONs in *this are still valid
  332.  
  333.        If you actually want to MOVE the elements, use MoveToTail instead.
  334.     */
  335.     BOOL AddTail(CBaseList *pList);
  336.  
  337.  
  338.     /* Mirror images of AddHead: */
  339.  
  340.     /* Add single object to become a new first element of the list.
  341.        Return the new head position, NULL if it fails.
  342.        Existing POSITIONs in *this are still valid
  343.     */
  344. protected:
  345.     POSITION AddHeadI(void * pObj);
  346. public:
  347.  
  348.     /* Add all the elements in *pList to the head of *this.
  349.        Same warnings apply as for AddTail.
  350.        Return TRUE if it all worked, FALSE if it didn't.
  351.        If it fails some of the objects may have been added.
  352.  
  353.        If you actually want to MOVE the elements, use MoveToHead instead.
  354.     */
  355.     BOOL AddHead(CBaseList *pList);
  356.  
  357.  
  358.     /* Add the object *pObj to *this after position p in *this.
  359.        AddAfter(NULL,x) adds x to the start - equivalent to AddHead
  360.        Return the position of the object added, NULL if it failed.
  361.        Existing POSITIONs in *this are undisturbed, including p.
  362.     */
  363. protected:
  364.     POSITION AddAfterI(POSITION p, void * pObj);
  365. public:
  366.  
  367.     /* Add the list *pList to *this after position p in *this
  368.        AddAfter(NULL,x) adds x to the start - equivalent to AddHead
  369.        Return TRUE if it all worked, FALSE if it didn't.
  370.        If it fails, some of the objects may be added
  371.        Existing POSITIONs in *this are undisturbed, including p.
  372.     */
  373.     BOOL AddAfter(POSITION p, CBaseList *pList);
  374.  
  375.  
  376.     /* Mirror images:
  377.        Add the object *pObj to this-List after position p in *this.
  378.        AddBefore(NULL,x) adds x to the end - equivalent to AddTail
  379.        Return the position of the new object, NULL if it fails
  380.        Existing POSITIONs in *this are undisturbed, including p.
  381.     */
  382.     protected:
  383.     POSITION AddBeforeI(POSITION p, void * pObj);
  384.     public:
  385.  
  386.     /* Add the list *pList to *this before position p in *this
  387.        AddAfter(NULL,x) adds x to the start - equivalent to AddHead
  388.        Return TRUE if it all worked, FALSE if it didn't.
  389.        If it fails, some of the objects may be added
  390.        Existing POSITIONs in *this are undisturbed, including p.
  391.     */
  392.     BOOL AddBefore(POSITION p, CBaseList *pList);
  393.  
  394.  
  395.     /* Note that AddAfter(p,x) is equivalent to AddBefore(Next(p),x)
  396.        even in cases where p is NULL or Next(p) is NULL.
  397.        Similarly for mirror images etc.
  398.        This may make it easier to argue about programs.
  399.     */
  400.  
  401.  
  402.  
  403.     /* The following operations do not copy any elements.
  404.        They move existing blocks of elements around by switching pointers.
  405.        They are fairly efficient for long lists as for short lists.
  406.        (Alas, the Count slows things down).
  407.  
  408.        They split the list into two parts.
  409.        One part remains as the original list, the other part
  410.        is appended to the second list.  There are eight possible
  411.        variations:
  412.        Split the list {after/before} a given element
  413.        keep the {head/tail} portion in the original list
  414.        append the rest to the {head/tail} of the new list.
  415.  
  416.        Since After is strictly equivalent to Before Next
  417.        we are not in serious need of the Before/After variants.
  418.        That leaves only four.
  419.  
  420.        If you are processing a list left to right and dumping
  421.        the bits that you have processed into another list as
  422.        you go, the Tail/Tail variant gives the most natural result.
  423.        If you are processing in reverse order, Head/Head is best.
  424.  
  425.        By using NULL positions and empty lists judiciously either
  426.        of the other two can be built up in two operations.
  427.  
  428.        The definition of NULL (see Next/Prev etc) means that
  429.        degenerate cases include
  430.           "move all elements to new list"
  431.           "Split a list into two lists"
  432.           "Concatenate two lists"
  433.           (and quite a few no-ops)
  434.  
  435.        !!WARNING!! The type checking won't buy you much if you get list
  436.        positions muddled up - e.g. use a POSITION that's in a different
  437.        list and see what a mess you get!
  438.     */
  439.  
  440.     /* Split *this after position p in *this
  441.        Retain as *this the tail portion of the original *this
  442.        Add the head portion to the tail end of *pList
  443.        Return TRUE if it all worked, FALSE if it didn't.
  444.  
  445.        e.g.
  446.           foo->MoveToTail(foo->GetHeadPosition(), bar);
  447.               moves one element from the head of foo to the tail of bar
  448.           foo->MoveToTail(NULL, bar);
  449.               is a no-op, returns NULL
  450.           foo->MoveToTail(foo->GetTailPosition, bar);
  451.               concatenates foo onto the end of bar and empties foo.
  452.  
  453.        A better, except excessively long name might be
  454.            MoveElementsFromHeadThroughPositionToOtherTail
  455.     */
  456.     BOOL MoveToTail(POSITION pos, CBaseList *pList);
  457.  
  458.  
  459.     /* Mirror image:
  460.        Split *this before position p in *this.
  461.        Retain in *this the head portion of the original *this
  462.        Add the tail portion to the start (i.e. head) of *pList
  463.  
  464.        e.g.
  465.           foo->MoveToHead(foo->GetTailPosition(), bar);
  466.               moves one element from the tail of foo to the head of bar
  467.           foo->MoveToHead(NULL, bar);
  468.               is a no-op, returns NULL
  469.           foo->MoveToHead(foo->GetHeadPosition, bar);
  470.               concatenates foo onto the start of bar and empties foo.
  471.     */
  472.     BOOL MoveToHead(POSITION pos, CBaseList *pList);
  473.  
  474.  
  475.     /* Reverse the order of the [pointers to] objects in *this
  476.     */
  477.     void Reverse();
  478.  
  479.  
  480.     /* set cursor to the position of each element of list in turn  */
  481.     #define TRAVERSELIST(list, cursor)               \
  482.     for ( cursor = (list).GetHeadPosition()           \
  483.         ; cursor!=NULL                               \
  484.         ; cursor = (list).Next(cursor)                \
  485.         )
  486.  
  487.  
  488.     /* set cursor to the position of each element of list in turn
  489.        in reverse order
  490.     */
  491.     #define REVERSETRAVERSELIST(list, cursor)        \
  492.     for ( cursor = (list).GetTailPosition()           \
  493.         ; cursor!=NULL                               \
  494.         ; cursor = (list).Prev(cursor)                \
  495.         )
  496.  
  497. }; // end of class declaration
  498.  
  499. template<class OBJECT> class CGenericList : public CBaseList
  500. {
  501. public:
  502.     CGenericList(TCHAR *pName,
  503.                  INT iItems,
  504.                  BOOL bLock = TRUE,
  505.                  BOOL bAlert = FALSE) :
  506.                      CBaseList(pName, iItems) {
  507.         UNREFERENCED_PARAMETER(bAlert);
  508.         UNREFERENCED_PARAMETER(bLock);
  509.     };
  510.     CGenericList(TCHAR *pName) :
  511.                      CBaseList(pName) {
  512.     };
  513.  
  514.     POSITION GetHeadPosition() const { return (POSITION)m_pFirst; }
  515.     POSITION GetTailPosition() const { return (POSITION)m_pLast; }
  516.     int GetCount() const { return m_Count; }
  517.  
  518.     OBJECT *GetNext(POSITION& rp) const { return (OBJECT *) GetNextI(rp); }
  519.  
  520.     OBJECT *Get(POSITION p) const { return (OBJECT *) GetI(p); }
  521.     OBJECT *GetHead() const  { return Get(GetHeadPosition()); }
  522.  
  523.     OBJECT *RemoveHead() { return (OBJECT *) RemoveHeadI(); }
  524.  
  525.     OBJECT *RemoveTail() { return (OBJECT *) RemoveTailI(); }
  526.  
  527.     OBJECT *Remove(POSITION p) { return (OBJECT *) RemoveI(p); }
  528.     POSITION AddBefore(POSITION p, OBJECT * pObj) { return AddBeforeI(p, pObj); }
  529.     POSITION AddAfter(POSITION p, OBJECT * pObj)  { return AddAfterI(p, pObj); }
  530.     POSITION AddHead(OBJECT * pObj) { return AddHeadI(pObj); }
  531.     POSITION AddTail(OBJECT * pObj)  { return AddTailI(pObj); }
  532.     BOOL AddTail(CGenericList<OBJECT> *pList)
  533.             { return CBaseList::AddTail((CBaseList *) pList); }
  534.     BOOL AddHead(CGenericList<OBJECT> *pList)
  535.             { return CBaseList::AddHead((CBaseList *) pList); }
  536.     BOOL AddAfter(POSITION p, CGenericList<OBJECT> *pList)
  537.             { return CBaseList::AddAfter(p, (CBaseList *) pList); };
  538.     BOOL AddBefore(POSITION p, CGenericList<OBJECT> *pList)
  539.             { return CBaseList::AddBefore(p, (CBaseList *) pList); };
  540.     POSITION Find( OBJECT * pObj) const { return FindI(pObj); }
  541. }; // end of class declaration
  542.  
  543.  
  544.  
  545. /* These define the standard list types */
  546.  
  547. typedef CGenericList<CBaseObject> CBaseObjectList;
  548. typedef CGenericList<IUnknown> CBaseInterfaceList;
  549.  
  550. #endif /* __WXLIST__ */
  551.  
  552.